package org.meteorshower.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;

public class RenterAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public RenterAuthenticationFilter(String RenterUrl) {
        super(RenterUrl);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
        String RoomId = httpServletRequest.getHeader("rId");
        String username = httpServletRequest.getHeader("username");
        String password = httpServletRequest.getHeader("password");

        RenterAuthenticationToken token = new RenterAuthenticationToken(username,password);
//        token.setDetails(Collections.singletonMap("rId",RoomId));
        token.setRId(RoomId);

        Authentication authentication = getAuthenticationManager().authenticate(token);
        return authentication;
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationManager authenticationManager){
        return authenticationManager;
    }
}
